﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MayhemWpf.UserControls;
using SensorMayhem.Events;
using Microsoft.Maps.MapControl.WPF;
using System.Device.Location;
using SensorMayhem.Helpers;

namespace SensorMayhem.Wpf
{
    /// <summary>
    /// Interaction logic for LocationConfig.xaml
    /// </summary>
    public partial class LocationConfig : WpfConfiguration
    {
        public LocationConfig(LocationOptions options)
        {
            InitializeComponent();
            circleLayer = new MapLayer();
            myMap.Children.Add(circleLayer);

            positionLayer = new MapLayer();
            myMap.Children.Add(positionLayer);

            Options = options;
            if (options.Location != null)
            { 
                Location = options.Location;
            }

            try
            {
                geoWatcher = new GeoCoordinateWatcher();
                geoWatcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(geoWatcher_PositionChanged);
                geoWatcher.TryStart(true, TimeSpan.FromMilliseconds(5000));
                var currentPosition = geoWatcher.Position;

                if (!currentPosition.Location.IsUnknown)
                {
                    UpdateCurrentPositionPushPin(currentPosition);
                    myMap.SetView(new Microsoft.Maps.MapControl.WPF.Location(currentPosition.Location.Latitude, currentPosition.Location.Longitude), 10);
                }
            }
            catch (Exception)
            {
            }

            RadiusSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(RadiusSlider_ValueChanged);
        }

        void geoWatcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            UpdateCurrentPositionPushPin(e.Position);
        }

        private void UpdateCurrentPositionPushPin(GeoPosition<GeoCoordinate> position)
        {
            var location = new Microsoft.Maps.MapControl.WPF.Location(position.Location.Latitude, position.Location.Longitude, position.Location.Altitude);

            Pushpin pin = new Pushpin();
            pin.Location = location;

            positionLayer.Children.Clear();
            positionLayer.AddChild(pin, location, PositionOrigin.BottomCenter);
        }

        private MapLayer circleLayer;

        private MapLayer positionLayer;

        private GeoCoordinateWatcher geoWatcher;


        public override string Title
        {
            get
            {
                return "Location";
            }
        }

        public LocationOptions Options
        {
            get;
            set;
        }

        public Microsoft.Maps.MapControl.WPF.Location Location
        {
            get;
            set;
        }

        public override void OnLoad()
        {
            RadiusSlider.Value = Options.Radius;

            if (Options.Inside)
            {
                insideRadioButton.IsChecked = true;
            }
            else
            {
                outsideRadioButton.IsChecked = true;
            }

            if (Options.Location != null)
            {
                Location = Options.Location;

                UpdateRadiusPolygon();
                this.selectedLocationTextBlock.Text = Location.ToString();

                CanSave = true;
            }

            
        }

        public override void OnSave()
        {
            Options.Inside = (bool)insideRadioButton.IsChecked;

            if (Location != null)
            {
                Options.Location = Location;
            }
            Options.Radius = RadiusSlider.Value;
        }

        private void UpdateRadiusPolygon()
        {
            if (Location != null)
            { 
                var circle = GeoCodeCalc.CreateCircle(Location, RadiusSlider.Value);
                var polygon = new MapPolygon();
                polygon.Locations = circle;
                polygon.Fill = Brushes.Orange;
                polygon.Opacity = 0.5;

                circleLayer.Children.Clear();
                circleLayer.Children.Add(polygon);
            }

        }

        private void myMap_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;

            Point mousePosition = e.GetPosition(myMap);
            Microsoft.Maps.MapControl.WPF.Location pinLocation = myMap.ViewportPointToLocation(mousePosition);

            Location = pinLocation;

            UpdateRadiusPolygon();

            this.selectedLocationTextBlock.Text = pinLocation.ToString();
            CanSave = true;
        }

        private void RadiusSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            UpdateRadiusPolygon();
        }

        
    }
}
